<?php
namespace AuctionCore\Account;


/**
* 帐户组 测试类
* @author       爱民
*/
class AuthTest extends \PHPUnit_Framework_TestCase
{

	private $group_id = null;
	private $rule_id = null;

	private function _testGroup(){
		$group_info = array();
		$group_info['title'] = 'test';
		$group_info['status'] = 1;
		$group_info['rules'] = '';
		$group_info['remark'] = '前台车主用户帐号组';

		//创建前台用户组
		$f_group =	new \AuctionCore\Account\Group();		 	
		$insertID = $f_group->create($group_info);
		$this->assertTrue(is_numeric($insertID));
		$this->group_id = $insertID;

		//更新组信息
		$f_group =	new \AuctionCore\Account\Group();		  		 	
		$group_info = array("title"=>'test_update');
		$ok = $f_group->update($group_info,"id='".$insertID."'");
		$this->assertEquals(1,$ok);
		$f_group->enable($insertID); //启用组
		$f_group->disable($insertID); //禁用组

		//分配组权限;
		$f_group =	new \AuctionCore\Account\Group();	  		 	
		$ok = $f_group->add_rule($insertID,35);
		$info = $f_group->info($insertID);
		$rules = explode(',',$info['rules']);
		$this->assertTrue(in_array(35, $rules));
		$ok = $f_group->add_rule($insertID,array(35,36,37,38));
		$info = $f_group->info($insertID);
		$rules = explode(',',$info['rules']);
		$this->assertTrue(in_array(35, $rules)&&in_array(37,$rules));
		$this->assertEquals(1,$ok);

		//移组权限
		$ok = $f_group->remove_rule($insertID,array(35,37));
		$info = $f_group->info($insertID);
		$rules = explode(',', $info['rules']);
		$this->assertTrue(!in_array(35, $rules)&&!in_array(37,$rules));
	}

	private function _clearGroup(){
		//删除前台用户组;
		$f_group =	new \AuctionCore\Account\Group();		 	
		$ok = $f_group->del($this->group_id);
		$this->assertEquals(1,$ok);
	}

	public function testGroup(){
		$this->_testGroup();
		$this->_clearGroup();
	}
	
	private function _testRule(){
		$rule_info = array();	
		$rule_info['name'] = 'owner_center';
		$rule_info['title'] = '车主中心';
		$rule_info['type'] = '1';
		$rule_info['status'] = '1';
		$rule_info['condition'] = '';

		//创建前台用户规则
		$f_Rule =	new \AuctionCore\Account\Rule();		 	
		$insertID = $f_Rule->create($rule_info);
		$this->assertTrue(is_numeric($insertID));
		$this->rule_id = $insertID;

		//更新规则信息
		$f_rule =	new \AuctionCore\Account\Rule();		  		 	
		$rule_info = array("title"=>'aaaaa');
		$ok = $f_rule->update($rule_info,"id='".$insertID."'");
		$this->assertEquals(1,$ok);
		$f_rule->enable($insertID); //启用规则
		$f_rule->disable($insertID); //禁用规则
	}

	private function _clearRule(){
		//删除前台用户规则
		$f_rule =	new \AuctionCore\Account\Rule();		 	
		$ok = $f_rule->del($this->rule_id);
		$this->assertEquals(1,$ok);
	}

	public function testRule(){
		$this->_testRule();
		$this->_clearRule();
	}


	public function testCheckUserRule(){
		//检查权限
		$this->_testRule();
		$this->_testGroup();
		//组分配权限
		$f_group =	new \AuctionCore\Account\Group();
		$ok = $f_group->add_rule($this->group_id,$this->rule_id);

		//创建前台用户	[用户名，密码,昵称,手机号]
		$user_info = array();
		$user_info['user_name'] = 'unit_test';
		$user_info['passwd'] = 'cccccc';
		$user_info['nick_name'] = 'test_nick_name';
		$user_info['sms_mobile'] = '13552528384';
		$user_info['pwd_mobile'] = '13552528384';
		$user_id = \AuctionCore\Account\FrontUser::create($user_info);

		//检查权限
		$fu = new \AuctionCore\Account\FrontUser('unit_test');
		$fu->set_group($this->group_id);         //添加用户属组
		$ok = $fu->check_auth("owner_center");  //检查权限
		$this->assertTrue($ok);

		$fu->exgraft_group($this->group_id);//移出组	
		\AuctionCore\Account\FrontUser::remove('unit_test'); //删除前台用户

		$this->_clearGroup();
		$this->_clearRule();
	}



	




}

?>